package ch.elektroplan.mueller.passwordSave.presentation;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ch.elektroplan.mueller.passwordSave.R;
import ch.elektroplan.mueller.passwordSave.R.string;
import ch.elektroplan.mueller.passwordSave.business.supporters.Enigma;
import ch.elektroplan.mueller.passwordSave.business.supporters.Password_DB_Generator;

public class Userdata extends Activity {

	private int raw;
	private EditText name, firstName, nickName, ip, password;;
	private Password_DB_Generator dbGenerator;
	private SQLiteDatabase database;
	private Enigma enigma;
	private Button saveButton, editButton, deleteButton;
	private Cursor cursor;

	private String currentName, currentFirstName, currentNickName, currentIp,
			currentPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userdata);

		this.dbGenerator = new Password_DB_Generator(this);
		this.enigma = new Enigma();

		try {
			this.raw = this.getIntent().getExtras().getInt(
					Password_DB_Generator.KEY, -1);
		} catch (Exception e) {
			Toast.makeText(this, "failed: raw is wrong", Toast.LENGTH_SHORT)
					.show();
		}

		this.name = (EditText) findViewById(R.id.tx_userdata_name);
		this.firstName = (EditText) findViewById(R.id.tx_userdata_firstname);
		this.nickName = (EditText) findViewById(R.id.tx_userdata_nickname);
		this.ip = (EditText) findViewById(R.id.tx_userdata_ip);
		this.password = (EditText) findViewById(R.id.tx_userdata_password);

		this.editButton = (Button) findViewById(R.id.bu_userdata_edit);
		this.saveButton = (Button) findViewById(R.id.bu_userdata_save);
		this.deleteButton = (Button) findViewById(R.id.bu_userdata_delete);

	}

	@Override
	protected void onPause() {
		database.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		database = dbGenerator.getReadableDatabase();


		String whereClause = Password_DB_Generator.NAME + " = '"
		+ this.getIntent().getExtras().getString("val") + "'";
		
		cursor= database.query(Password_DB_Generator.USERTABLE, dbGenerator.getAllColumns(), whereClause,null, null, null, null);

		cursor.moveToFirst();
		
		this.setupcurrentValues();

		this.name.setText(currentName);
		this.name.setEnabled(false);
		this.firstName.setText(currentFirstName);
		this.firstName.setEnabled(false);
		this.nickName.setText(currentNickName);
		this.nickName.setEnabled(false);
		this.ip.setText(currentIp);
		this.ip.setEnabled(false);
		this.password.setText(currentPassword);
		this.password.setEnabled(false);

		this.editButton.setOnClickListener(new EditButtonlistener());
		this.saveButton.setOnClickListener(new SaveButtonlistener());
		this.deleteButton.setOnClickListener(new DeleteButtonlistener());

	}

	private void setupcurrentValues() {

		this.currentName = enigma.decode(cursor.getString(cursor
				.getColumnIndexOrThrow(Password_DB_Generator.NAME)));
		this.currentFirstName = enigma.decode(cursor.getString(cursor
				.getColumnIndexOrThrow(Password_DB_Generator.FIRSTNAME)));
		this.currentNickName = enigma.decode(cursor.getString(cursor
				.getColumnIndexOrThrow(Password_DB_Generator.USERNAME)));
		this.currentIp = enigma.decode(cursor.getString(cursor
				.getColumnIndexOrThrow(Password_DB_Generator.IP)));
		this.currentPassword = enigma.decode(cursor.getString(cursor
				.getColumnIndexOrThrow(Password_DB_Generator.PASSWORD)));
	}

	private class SaveButtonlistener implements OnClickListener {

		public void onClick(View v) {
			editButton.setEnabled(true);
			deleteButton.setEnabled(true);
			saveButton.setEnabled(false);

			this.deactivateEditText(name);
			this.deactivateEditText(firstName);
			this.deactivateEditText(nickName);
			this.deactivateEditText(ip);
			this.deactivateEditText(password);

			try {
				String whereClause = Password_DB_Generator.NAME + " = '"
				+ enigma.encode(currentName) + "'";

				ContentValues cv = new ContentValues();
				cv.put(Password_DB_Generator.NAME, enigma.encode(name.getText().toString()));
				cv.put(Password_DB_Generator.FIRSTNAME, enigma.encode(firstName.getText().toString()));
				cv.put(Password_DB_Generator.USERNAME,enigma.encode(nickName.getText().toString()));
				cv.put(Password_DB_Generator.IP, enigma.encode(ip.getText().toString()));
				cv.put(Password_DB_Generator.PASSWORD, enigma.encode(password.getText().toString()));
				
				database.update(Password_DB_Generator.USERTABLE, cv, whereClause, null);
				
				onResume();

			} catch (Exception e) {
				
				Toast.makeText(Userdata.this, "failed: update db",
						Toast.LENGTH_SHORT).show();
			}
		}

		private void deactivateEditText(EditText edittext) {
			edittext.setEnabled(false);
			edittext.setBackgroundColor(Color.BLACK);
			edittext.setTextColor(Color.WHITE);

		}

	}

	private class EditButtonlistener implements OnClickListener {

		public void onClick(View v) {

			editButton.setEnabled(false);
			deleteButton.setEnabled(false);
			saveButton.setEnabled(true);

			activateEdittext(name);
			activateEdittext(firstName);
			activateEdittext(nickName);
			activateEdittext(ip);
			activateEdittext(password);
		}

		private void activateEdittext(EditText edittext) {

			edittext.setEnabled(true);
			edittext.setTextColor(Color.BLACK);
			edittext.setBackgroundColor(Color.WHITE);
		}

	}

	private class DeleteButtonlistener implements OnClickListener {

		public void onClick(View v) {

			try {

				String whereClause = Password_DB_Generator.NAME + " = '"
						+ enigma.encode(currentName) + "'";

				database.delete(Password_DB_Generator.USERTABLE, whereClause,
						null);

				Toast.makeText(Userdata.this, whereClause, Toast.LENGTH_SHORT)
						.show();
				Userdata.this.finish();

			} catch (Exception e) {

				Toast.makeText(Userdata.this, "fail:  delete",
						Toast.LENGTH_SHORT).show();

			}
		}
	}

}
